﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Goldlight.Mvvm.Base;
using Goldlight.Mvvm.Base.Infrastructure;
using Goldlight.SampleApplication.Views.Marketing;
using Goldlight.SampleApplication.Model;
using Goldlight.Mvvm.Base.Command;
using System.ComponentModel;
using System.Windows.Input;

namespace Goldlight.SampleApplication.ViewModels.Marketing
{
    /// <summary>
    /// The list of contacts available in Goldlight.
    /// </summary>
    public class ContactViewModel : WorkspaceViewModelBase, IEditObject
    {
        private LanguageResources.Language _language;
        private RangeObservableCollection<Goldlight.SampleApplication.Model.CustomerView> _customers;
        private RelayCommand _addItem;


        /// <summary>
        /// Initialize a new instance of <see cref="ContactViewModel"/>
        /// </summary>
        public ContactViewModel()
            : base()
        {
            Name = LanguageResources.Language.Contacts;
            IsCloseable = true;
            _language = new Goldlight.SampleApplication.LanguageResources.Language();
        }

        /// <summary>
        /// Get the resources for this application.
        /// </summary>
        public override object Resources
        {
            get
            {
                return _language;
            }
        }

        public RangeObservableCollection<Goldlight.SampleApplication.Model.CustomerView> Customers
        {
            get { return _customers; }
        }

        public RelayCommand AddItemCommand
        {
            get
            {
                if (_addItem == null)
                {
                    _addItem = new RelayCommand(() => Mediator.NotifyColleagues(MediatorMessages.NewContact, this));
                }
                return _addItem;
            }
        }

        public RelayCommand EditItemCommand
        {
            get { throw new NotImplementedException(); }
        }

        public RelayCommand DeleteItemCommand
        {
            get { throw new NotImplementedException(); }
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();

            Name = LanguageResources.Language.CustomersHeading;

            if (!this.IsInDesignMode)
            {
                Cursor cursor = Mouse.OverrideCursor;
                try
                {
                    Mouse.OverrideCursor = Cursors.Wait;
                    _customers = new RangeObservableCollection<Goldlight.SampleApplication.Model.CustomerView>();
                    BackgroundWorker worker = new BackgroundWorker();
                    worker.DoWork += new DoWorkEventHandler(worker_DoWork);
                    worker.RunWorkerAsync();
                }
                finally
                {
                    Mouse.OverrideCursor = null;
                }
            }
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            // Fill the customers in.
            var p = (from ctx in new GoldlightModelDataContext().CustomerViews
                     select ctx).ToList();
            _customers.AddRange(p);
            e.Result = true;
        }

        private void ShowContactManagement()
        {
            ContactManagementViewModel cvm = new ContactManagementViewModel();
            WorkspaceManager.AddWorkspace(cvm);
        }
    }
}
